<?php
/**
 * Date: 2019/4/16 17:34
 */

namespace App\Tools;

use Symfony\Component\VarDumper\VarDumper;

class LinkedList {
    public $_firstNode = NULL;
    public $_totalNode = 0;

    public function insert(string $data = NULL) {
        $newNode = new ListNode($data);

        if ($this->_firstNode === NULL) {
            $this->_firstNode = &$newNode;
        } else {
            $currentNode = $this->_firstNode;
            while ($currentNode->next !== NULL) {
                $currentNode = $currentNode->next;
            }
            $currentNode->next = $newNode;
        }

        $this->_totalNode++;

        return TRUE;
    }

    public function display() {
        echo "Total book titles: ".$this->_totalNode."\n";
        $currentNode = $this->_firstNode;
        while ($currentNode !== NULL) {
            VarDumper::dump($currentNode->val);
            $currentNode = $currentNode->next;
        }
    }
}

